Skip to content

fix(core): skip counting bytes past the last newline - #61449

Open
andreivince wants to merge 4 commits into
anomalyco:devfrom
andreivince:bounded-line-reader
Open

andreivince wants to merge 4 commits into
anomalyco:devfrom
andreivince:bounded-line-reader

Conversation

@andreivince

@andreivince andreivince commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Issue for this PR

Closes #61448. Follow-up to #63030.

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

After #63030, the reader still goes through every byte in a chunk to count newlines. Moving the existing last newline lookup before that loop lets it skip the count in chunks with no newline and stop at the last newline in the rest.

On my Mac, the 64 MiB repro from #61448 took about 98 ms on dev 2f886a2a5e and 43 ms with this patch, over 12 runs each.

How did you verify your code works?

The reader tests passed on macOS, Linux, and Windows. I also checked the long line, continuation, and both forms of tail read through the CLI on all three. The full Windows Core run hit a timeout in a Git packing test.

Screenshots / recordings

Not applicable.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Copilot AI balanced review requested due to automatic review settings November 3, 2026 16:45

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I ran the reader on the 64 MiB single-line file from #61448 with limit: 2. On the base it took about 5.7 s and 685 MiB of memory. With this change it took 89 ms and 226 MiB, and the returned page was the same. I also compared both versions on 300 random files. They mixed very long lines that span several chunks, multi-byte and cut-off UTF-8 characters, BOMs, CRLF line endings and NUL bytes, with random offsets and limits. Every page and error matched the base exactly, including the line counts in out-of-range errors and the old rule that strips a BOM from the selected line. The reader tests pass and typecheck:tsc passes.

The public API is unchanged. Only internal helpers were removed and an internal type narrowed.

I have no blocking concerns.

@andreivince andreivince changed the title fix(core): avoid rebuilding long lines during file reads fix(core): skip counting bytes past the last newline Nov 13, 2026

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new commits look correct. Counting 1 + newlines before lastIndexOf(10) gives the same total as counting the whole chunk, and chunks with no newline are now skipped.

On the #61448 script with a 64 MiB file, the base (which already has #63030) took about 172–175 ms and this change took 82–90 ms. Memory and the number of chunk reads were the same. I also compared both versions on 300 random files, about half of them read from the end with negative offsets. They included long lines spanning several chunks, multi-byte characters, BOMs, CRLF line endings and NUL bytes. Every page and error matched the base. The reader tests pass and typecheck:tsc passes.

Note that the slowdown and memory growth described in #61448 no longer reproduce on the base. #63030 already fixed them, so this change is a speedup on top of that.

The public API is unchanged. I have no concerns.

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only new commit merges dev into the branch and leaves read-filesystem.ts unchanged, so the change is the same one I reviewed before. On the new head, the reader tests still pass and the #61448 64 MiB script returns the same output in about 58 ms. The public API is unchanged. I have no concerns.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Paged reads repeatedly rebuild very long lines

2 participants